# Version-1
# INCS := -I./func
# SRCS := $(wildcard *.c)
# SRCS += $(wildcard ./func/*.c)

# # 命令的缩进需要使用tab，不能使用空格
# main : $(SRCS)
# 	gcc $(SRCS) $(INCS) -o main  

# Version-2
# CFLAGS = -I./func
# SRCS   := $(wildcard *.c)
# SRCS   += $(wildcard ./func/*.c)

# # 命令的缩进需要使用tab，不能使用空格
# main : $(SRCS)
# 	gcc $(CFLAGS) $^ -o $@  

# Version-3
# INCS      = ./func
# SRC_PATH  = .
# SRC_PATH += ./func   

# CFLAGS   = $(foreach dir, $(INCS), -I$(dir))
# SRCS     = $(foreach dir, $(SRC_PATH), $(wildcard $(dir)/*.c))

# main : $(SRCS)
# 	gcc $(CFLAGS) $^ -o $@  


# Version-4
# INCS      = ./func
# SRC_PATH  = .
# SRC_PATH += ./func   

# CFLAGS   := $(foreach dir, $(INCS), -I$(dir))
# SRCS     := $(foreach dir, $(SRC_PATH), $(wildcard $(dir)/*.c))
# OBJS     := $(patsubst %.c,%.o,$(SRCS))

# main : ${OBJS}
# 	gcc $^ -o $@


# %.o : %.c
# 	gcc -c $(CFLAGS) $< -o $@

# clean:
# 	rm -rf main
# 	rm -rf *.o
# 	rm -rf func/*.o

# Version-5
# INCS      = ./func
# SRC_PATH  = .
# SRC_PATH += ./func   
# OUTPUT   := build

# CFLAGS   := $(foreach dir, $(INCS), -I$(dir))
# SRCS     := $(foreach dir, $(SRC_PATH), $(wildcard $(dir)/*.c))
# OBJS     := $(patsubst %.c, $(OUTPUT)/%.o, $(SRCS))

# .PHONY : main clean

# main : ${OBJS}
# 	mkdir bin
# 	gcc $^ -o bin/$@

# $(OUTPUT)/%.o : %.c
# 	mkdir -p $(dir $@)         # 1. dir 文件或得到该问价的路径， 2.makefile中可以使用linux命令
# 	gcc -c $(CFLAGS) $< -o $@

# clean:
# 	rm -rf build
# 	rm -rf bin

# Version-6
# INCS      = ./func
# SRC_PATH  = .
# SRC_PATH += ./func   
# OUTPUT   := build

# CFLAGS   := $(foreach dir, $(INCS), -I$(dir))
# SRCS     := $(foreach dir, $(SRC_PATH), $(wildcard $(dir)/*.c))
# OBJS     := $(patsubst %.c, $(OUTPUT)/%.o, $(SRCS))

# .PHONY : main clean

# main : ${OBJS}
# 	@echo 可重定位文件编译完成，开始链接...
# 	@mkdir bin
# 	@gcc $^ -o bin/$@
# 	@echo 编译完成!

# $(OUTPUT)/%.o : %.c
# 	@mkdir -p $(dir $@)        
# 	@gcc -c $(CFLAGS) $< -o $@

# clean:
# 	@echo 清理工作开始...
# 	@rm -rf build
# 	@rm -rf bin
# 	@echo 清理结束!!!


# Version-7
ROOT     := $(shell pwd)
INCS      = $(ROOT)/func
SRC_PATH  = $(ROOT)
SRC_PATH += $(ROOT)/func   
OUTPUT   := $(ROOT)/build
BIN      := $(ROOT)/bin
TARGET   := main

CFLAGS   := $(foreach dir, $(INCS), -I$(dir))
SRCS     := $(foreach dir, $(SRC_PATH), $(wildcard $(dir)/*.c))
OBJS     := $(patsubst $(ROOT)/%.c, $(OUTPUT)/%.o, $(SRCS))

.PHONY : $(TARGET) clean

$(TARGET) : ${OBJS}
	@echo 可重定位文件编译完成，开始链接...
	@mkdir $(BIN)
	@gcc $^ -o $(BIN)/$@
	@echo 编译完成!

$(OUTPUT)/%.o : %.c
	@mkdir -p $(dir $@)        
	@gcc -c $(CFLAGS) $< -o $@

clean:
	@echo 清理工作开始...
	@rm -rf $(OUTPUT)
	@rm -rf $(BIN)
	@echo 清理结束!!!